package com.edu.demo.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.demo.entity.UsersFriend;
import com.edu.demo.entity.dto.UserLoginDTO2;
import com.edu.demo.entity.vo.UsersFriendByAddVO;
import com.edu.demo.entity.vo.UsersFriendUpdateVO;
import com.edu.demo.result.MyPage;
import com.edu.demo.result.Result;
import com.edu.demo.service.IUsersFriendService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 好友关系 前端控制器
 * </p>
 *
 * @author Ashe1y
 * @since 2024-03-13
 */
@Api(tags = "好友关系")
@RestController
@RequestMapping("/users-friend")
public class UsersFriendController {

    @Resource
    private IUsersFriendService usersFriendService;


    /**
     * 查询所有接口
     *
     * @return
     */
    @ApiOperationSupport(author = "Ashe1y")
    @ApiOperation(value = "查询好友关系所有数据")
    @GetMapping
    public Result<List<UsersFriend>> findAll() {
        return Result.success(usersFriendService.list()).msg("查询成功");
    }

    /**
     * 根据id查询数据接口
     *
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Ashe1y")
    @ApiOperation(value = "根据id查询数据接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "组件id", required = true)
    })
    @GetMapping("/{id}")
    public Result<UsersFriend> findOne(@PathVariable Integer id) {
        return Result.success(usersFriendService.getById(id)).msg("查询成功");
    }

    /**
     * 分页查询接口
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperationSupport(author = "Ashe1y")
    @ApiOperation(value = "分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", example = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页多少条", example = "10", required = true)
    })
    @GetMapping("/page")
    public Result<MyPage<UsersFriend>> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        QueryWrapper<UsersFriend> queryWrapper = new QueryWrapper<>();
        return Result.success().data(usersFriendService.page(new Page<>(pageNum, pageSize), queryWrapper)).msg("查询成功");
    }

    /**
     * 查询好友通知
     *
     * @return
     */
    @ApiOperationSupport(author = "Ashe1y")
    @ApiOperation(value = "查询好友通知")
    @GetMapping("/findFriendMsg")
    public Result<UserLoginDTO2> findFriendMsg() {
        System.out.println("StpUtil.getLoginIdDefaultNull() = " + StpUtil.getLoginIdDefaultNull());
        if (StpUtil.getLoginIdDefaultNull() == null) {
            return Result.error().code(1006).msg("请登录");
        }
        List<UserLoginDTO2> friend = usersFriendService.findFriend(StpUtil.getLoginIdAsInt());
        System.out.println("friend = " + friend);
        return Result.success().msg("查询成功").data(friend);
    }

    /**
     * 查询好友列表
     *
     * @return
     */
    @ApiOperationSupport(author = "Ashe1y")
    @ApiOperation(value = "查询好友列表")
    @GetMapping("/findFriendList")
    public Result<UserLoginDTO2> findFriendList() {
        System.out.println("StpUtil.getLoginIdDefaultNull() = " + StpUtil.getLoginIdDefaultNull());
        if (StpUtil.getLoginIdDefaultNull() == null) {
            return Result.error().code(1006).msg("请登录");
        }
        List<UserLoginDTO2> friendList = usersFriendService.findFriendList(StpUtil.getLoginIdAsInt());
        return Result.success().msg("查询成功").data(friendList);
    }

    /**
     * 添加接口
     *
     * @param
     * @return
     */
    @ApiOperationSupport(author = "Ashe1y")
    @ApiOperation(value = "添加")
    @PostMapping("/add")
    public Result<String> add(@Valid @RequestBody UsersFriendByAddVO usersFriendByAddVO) {
        if (StpUtil.getLoginIdDefaultNull() == null) {
            return Result.error().code(1006).msg("请登录");
        }
        UsersFriend usersFriend = new UsersFriend();
        BeanUtil.copyProperties(usersFriendByAddVO, usersFriend);
        usersFriend.setUserId(StpUtil.getLoginIdAsInt());
        System.out.println("usersFriend = " + usersFriend);
        usersFriendService.addFriend(usersFriend);
        return Result.success().msg("添加成功");
    }

    /**
     * 更新接口
     *
     * @param
     * @return
     */
    @ApiOperationSupport(author = "Ashe1y")
    @ApiOperation(value = "更新接口")
    @PutMapping("/update")
    public Result<String> update(@RequestBody UsersFriendUpdateVO usersFriendUpdateVO) {
        if (StpUtil.getLoginIdDefaultNull() == null) {
            return Result.error().code(1006).msg("请登录");
        }
        usersFriendUpdateVO.setFriendId(StpUtil.getLoginIdAsInt());
        UsersFriend usersFriend = new UsersFriend();
        BeanUtil.copyProperties(usersFriendUpdateVO, usersFriend);
        usersFriendService.friendOperate(usersFriend);
        return Result.success().msg("修改成功");
    }


    /**
     * 删除接口
     *
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Ashe1y")
    @ApiOperation(value = "删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "组件id", required = true)
    })
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Integer id) {
        usersFriendService.removeById(id);
        return Result.success().msg("删除成功");
    }

    /**
     * 批量删除接口
     *
     * @param ids
     * @return
     */
    @ApiOperationSupport(author = "Ashe1y")
    @ApiOperation(value = "批量删除接口")
    @PostMapping("/del/batch")
    public Result<String> deleteBatch(@RequestBody List<Integer> ids) {
        usersFriendService.removeByIds(ids);
        return Result.success().msg("批量删除成功");
    }
}